package com.presalept.manipulatedata;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.presalept.polodo.UserDO;

public class CreateUser {
	
	public static Integer InsertUser(UserDO userdo) throws SQLException{
		
		Integer InsertResult = 0;
		
		Connection dbConn = null;
		PreparedStatement dbPreStmt = null;
	
		String dbSql = "Insert into userinfo(username, password, email, joindate) values(?, ?, ?, ?)";
		
		try{
			
			dbConn = DbManager.getConnection();
			
			dbPreStmt = dbConn.prepareStatement(dbSql);
			
			dbPreStmt.setString(1, userdo.getUserName());
			dbPreStmt.setString(2, userdo.getPassword());
			dbPreStmt.setString(3, userdo.getEmail());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	        Date createdate = null;  
	        try {  
	        		createdate = sdf.parse(userdo.getJoinDate()); 
	            
	        } catch (ParseException e) {  
	            e.printStackTrace();  
	        }     
	        java.sql.Date sqlcreateDate = new java.sql.Date(createdate.getTime()); 
	        dbPreStmt.setDate(4, sqlcreateDate);
	        
	        InsertResult = dbPreStmt.executeUpdate();

			
		}finally{
			
			if (dbConn != null) dbConn.close();
			
		}

		return InsertResult;
		
	}

}
